using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;

namespace FeedFly
{
    /// <summary>
    /// Feed subclass for Atom feeds
    /// </summary>
    public class FeedAtom: Feed
    {
        public FeedAtom(int folderId, string address, short itemLimit, XmlReader feedReader)
            : base(folderId, address, itemLimit, feedReader)
        {
            // set feed format
            this.format = FeedFormat.Atom;
        }

        public FeedAtom(FeedInterface feed)
            : base(feed)
        { }

        protected override XmlReader AdvanceReaderToItems(XmlReader feedReader)
        {
            feedReader.ReadToFollowing("entry");
            return feedReader;
        }

        protected override void Parse(XmlReader feedReader)
        {
            while (feedReader.Read())
            {
                if (feedReader.NodeType == XmlNodeType.Element)
                {
                    switch (feedReader.Name)
                    {
                        case "title":
                            this.title = feedReader.ReadString();
                            break;
                        case "link":
                            this.link = feedReader.GetAttribute("href");
                            break;
                        case "entry":
                            this.AddItem(this.ParseItem(feedReader));
                            break;
                        default:
                            feedReader.ReadString();
                            break;
                    }
                }
            }

            feedReader.Close();
        }

        protected override FeedItem ParseItem(XmlReader feedReader)
        {
            FeedItem item = new FeedItem(this);

            while (feedReader.Read())
            {
                if (feedReader.NodeType == XmlNodeType.Element)
                {
                    switch (feedReader.Name)
                    {
                        case "title":
                            item.Title = feedReader.ReadString();
                            break;
                        case "id":
                            item.Guid = feedReader.ReadString();
                            break;
                        case "link":
                            item.Link = feedReader.ReadString();
                            break;
                        case "published":
                        case "created":
                            item.PubDate = Feed.ParseDate(feedReader.ReadString());
                            break;
                        case "author":
                            // advance feed to internal name element
                            feedReader.Read();
                            item.Author = feedReader.ReadString();
                            feedReader.Read();
                            break;
                        case "content":
                            item.Description = feedReader.ReadString();
                            break;
                        default:
                            feedReader.ReadString();
                            break;
                    }
                }
                else if (feedReader.NodeType == XmlNodeType.EndElement && feedReader.Name == "entry")
                {
                    if (item.PubDate == DateTime.MinValue)
                        item.PubDate = DateTime.Now;
                    break;
                }
            }

            return item;
        }
    }
}
